<!-- Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -->
<!DOCTYPE html>
<title>Unit Test of e2e.Cipher.RSA</title>
<script src="../../../closure/base.js"></script>
<script src="test_js_deps-runfiles.js"></script>
<script>
  goog.require('e2e.cipher.RSA');
  goog.require('goog.array');
  goog.require('goog.testing.jsunit');
</script>
<script>
function testBasic() {
  var n = [0x01, 0x7d, 0x24, 0x9a, 0x13, 0x08, 0x59, 0x78, 0x1c, 0xc0,
           0x2f, 0xcf, 0xb3, 0xc0, 0xdc, 0x7a, 0xe3, 0x79, 0xb2, 0xb4,
           0x59, 0xdf, 0x20, 0x07, 0xbb, 0x4d, 0x7e, 0x5e, 0xc0, 0xa6,
           0xd9, 0x06, 0x25];
  var e = [0x1, 0x00, 0x01];
  var d = [0xe5, 0x6a, 0xda, 0xb3, 0x0a, 0x1b, 0x90, 0x88, 0xfd, 0xc5,
           0xc9, 0xa7, 0xd1, 0x36, 0xe5, 0x5f, 0xff, 0x15, 0x5e, 0x12,
           0xe8, 0x18, 0xee, 0x4f, 0x66, 0x19, 0xe6, 0x6b, 0x12, 0x0b,
           0x36, 0x21];
  var plaintext = [0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02];
  var rsa = new e2e.cipher.RSA(
    e2e.cipher.Algorithm.RSA,
    {n: n, e: e, d: d});
  var ciphertext = e2e.async.Result.getValue(rsa.encrypt(plaintext))['c'];
  assertArrayEquals(
    [0x6c,0xd2,0xfb,0xdb,0x12,0xc2,0x87,0x2e,0x4e,0xd1,0x50,0x2a,0xa5,
     0x88,0x2a,0xc5,0x48,0xd0,0x56,0x00,0x1f,0x22,0x12,0x66,0xa6,0x11,
     0xbd,0x29,0xa2,0x80,0x2a,0x85], ciphertext);

  var recovered = e2e.async.Result.getValue(rsa.decrypt({c:ciphertext}));
  assertArrayEquals(plaintext, recovered);
};


function testBlinding() {
// vectors from ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15crypt-vectors.txt
  var n = [0xa8, 0xb3, 0xb2, 0x84, 0xaf, 0x8e, 0xb5, 0x0b, 0x38, 0x70, 0x34,
           0xa8, 0x60, 0xf1, 0x46, 0xc4, 0x91, 0x9f, 0x31, 0x87, 0x63, 0xcd,
           0x6c, 0x55, 0x98, 0xc8, 0xae, 0x48, 0x11, 0xa1, 0xe0, 0xab, 0xc4,
           0xc7, 0xe0, 0xb0, 0x82, 0xd6, 0x93, 0xa5, 0xe7, 0xfc, 0xed, 0x67,
           0x5c, 0xf4, 0x66, 0x85, 0x12, 0x77, 0x2c, 0x0c, 0xbc, 0x64, 0xa7,
           0x42, 0xc6, 0xc6, 0x30, 0xf5, 0x33, 0xc8, 0xcc, 0x72, 0xf6, 0x2a,
           0xe8, 0x33, 0xc4, 0x0b, 0xf2, 0x58, 0x42, 0xe9, 0x84, 0xbb, 0x78,
           0xbd, 0xbf, 0x97, 0xc0, 0x10, 0x7d, 0x55, 0xbd, 0xb6, 0x62, 0xf5,
           0xc4, 0xe0, 0xfa, 0xb9, 0x84, 0x5c, 0xb5, 0x14, 0x8e, 0xf7, 0x39,
           0x2d, 0xd3, 0xaa, 0xff, 0x93, 0xae, 0x1e, 0x6b, 0x66, 0x7b, 0xb3,
           0xd4, 0x24, 0x76, 0x16, 0xd4, 0xf5, 0xba, 0x10, 0xd4, 0xcf, 0xd2,
           0x26, 0xde, 0x88, 0xd3, 0x9f, 0x16, 0xfb ];
  var e = [0x01, 0x00, 0x01];
  var d = [0x53, 0x33, 0x9c, 0xfd, 0xb7, 0x9f, 0xc8, 0x46, 0x6a, 0x65, 0x5c,
           0x73, 0x16, 0xac, 0xa8, 0x5c, 0x55, 0xfd, 0x8f, 0x6d, 0xd8, 0x98,
           0xfd, 0xaf, 0x11, 0x95, 0x17, 0xef, 0x4f, 0x52, 0xe8, 0xfd, 0x8e,
           0x25, 0x8d, 0xf9, 0x3f, 0xee, 0x18, 0x0f, 0xa0, 0xe4, 0xab, 0x29,
           0x69, 0x3c, 0xd8, 0x3b, 0x15, 0x2a, 0x55, 0x3d, 0x4a, 0xc4, 0xd1,
           0x81, 0x2b, 0x8b, 0x9f, 0xa5, 0xaf, 0x0e, 0x7f, 0x55, 0xfe, 0x73,
           0x04, 0xdf, 0x41, 0x57, 0x09, 0x26, 0xf3, 0x31, 0x1f, 0x15, 0xc4,
           0xd6, 0x5a, 0x73, 0x2c, 0x48, 0x31, 0x16, 0xee, 0x3d, 0x3d, 0x2d,
           0x0a, 0xf3, 0x54, 0x9a, 0xd9, 0xbf, 0x7c, 0xbf, 0xb7, 0x8a, 0xd8,
           0x84, 0xf8, 0x4d, 0x5b, 0xeb, 0x04, 0x72, 0x4d, 0xc7, 0x36, 0x9b,
           0x31, 0xde, 0xf3, 0x7d, 0x0c, 0xf5, 0x39, 0xe9, 0xcf, 0xcd, 0xd3,
           0xde, 0x65, 0x37, 0x29, 0xea, 0xd5, 0xd1];
  var p = [0xd3, 0x27, 0x37, 0xe7, 0x26, 0x7f, 0xfe, 0x13, 0x41, 0xb2, 0xd5,
           0xc0, 0xd1, 0x50, 0xa8, 0x1b, 0x58, 0x6f, 0xb3, 0x13, 0x2b, 0xed,
           0x2f, 0x8d, 0x52, 0x62, 0x86, 0x4a, 0x9c, 0xb9, 0xf3, 0x0a, 0xf3,
           0x8b, 0xe4, 0x48, 0x59, 0x8d, 0x41, 0x3a, 0x17, 0x2e, 0xfb, 0x80,
           0x2c, 0x21, 0xac, 0xf1, 0xc1, 0x1c, 0x52, 0x0c, 0x2f, 0x26, 0xa4,
           0x71, 0xdc, 0xad, 0x21, 0x2e, 0xac, 0x7c, 0xa3, 0x9d];
  var q = [0xcc, 0x88, 0x53, 0xd1, 0xd5, 0x4d, 0xa6, 0x30, 0xfa, 0xc0, 0x04,
           0xf4, 0x71, 0xf2, 0x81, 0xc7, 0xb8, 0x98, 0x2d, 0x82, 0x24, 0xa4,
           0x90, 0xed, 0xbe, 0xb3, 0x3d, 0x3e, 0x3d, 0x5c, 0xc9, 0x3c, 0x47,
           0x65, 0x70, 0x3d, 0x1d, 0xd7, 0x91, 0x64, 0x2f, 0x1f, 0x11, 0x6a,
           0x0d, 0xd8, 0x52, 0xbe, 0x24, 0x19, 0xb2, 0xaf, 0x72, 0xbf, 0xe9,
           0xa0, 0x30, 0xe8, 0x60, 0xb0, 0x28, 0x8b, 0x5d, 0x77];
  var plaintext = [0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
                   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02];
  var rsa = new e2e.cipher.RSA(
    e2e.cipher.Algorithm.RSA,
    {n: n, e: e, d: d, p: p, q: q});
  assertTrue(rsa.use_blinding);
  var ciphertext = e2e.async.Result.getValue(rsa.encrypt(plaintext))['c'];
  var recovered = e2e.async.Result.getValue(rsa.decrypt({c:ciphertext}));
  assertArrayEquals(plaintext, recovered);
  // a few times more to make sure RSA blinding works consistently
  for (var i = 0; i < 10; i++) {
    var recovered = e2e.async.Result.getValue(rsa.decrypt({c:ciphertext}));
    assertArrayEquals(plaintext, recovered);
  }

};

</script>
